/******************************************************************************
* File description: IndexIdentificationPlanTable.qml
* Function: 主页检测方案表格
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-20
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import Qt.labs.qmlmodels 1.0

Item {
    id: root

    /* property 定义开始 */
    // 按钮文字和选中状态
    property int quanBuCount: 0
    property bool quanBuCheck: true
    property int xuXiShiCount: 0
    property bool xuXiShiCheck: false
    property int ySTRCount: 0
    property bool ySTRCheck: false
    property int miniSTRCount: 0
    property bool miniSTRCheck: false

    // 表格数据
    property var tableModels: []

    // 私有属性
    // 起始表头
    property var _startHorHeaderData: ["序号", "孔位", "样本名称", "检测方案", "总稀释倍数\nDFa"];
    // 末尾表头
    property var _endHorHeaderData: ["扩增上样\n体积", "备注"];
    // 一级表头
    property var _firstHorHeaderData: ["第一步稀释步骤", "第二步稀释步骤"];
    // 二级表头
    property var _secondHorHeaderData: ["稀释倍数1\nDFa1", "样品体积\n(μl)", "稀释液体积\n(μl)",
        "稀释倍数2\nDFa2", "上一步稀释\n产物(μl)", "稀释液体积\n(μl)"];
    // 非二级表头高度
    property int _horHeaderHeight: 76
    // 二级表头
    // 一级表头高度
    property int _firstHorHeaderHeight: 32
    // 二级表头高度
    property int _secondHorHeaderHeight: 44

    // 表格
    property int _tableItemHeight: 40
    property var _tableModel: tableModels
    // 暂存的table数据
    property var _savedTableModel: null

    // 信号
    // 外部信号
    signal filterData(int index); // 过滤数据

    // 函数
    // 过滤按钮点击逻辑处理
    function filterBtnClicked(index, isFilter = true){
        quanBuCheck = (index === 0)
        miniSTRCheck = (index === 1)
        ySTRCheck = (index === 2)
        xuXiShiCheck = (index === 3)

        // 过滤数据
        // if(isFilter) root.filterData(index)
    }

    // 函数
    // 重置表格位置
    function resetTableViewPosition(posX, posY){
        if(posX !== undefined && posX !== null){
            tableContent.contentX = posX
        }
        if(posY !== undefined && posY !== null){
            tableContent.contentY = posY
        }
    }

    // 设置过滤数据
    function setFilterData(filterData){
        if(filterData)
        {
            var jsonData = JSON.parse(filterData)
            if(jsonData)
            {
                quanBuCount = jsonData["全部"]
                miniSTRCount = jsonData["mini-STR"]
                ySTRCount = jsonData["Y-STR"]
                xuXiShiCount = jsonData["需稀释"]
            }
        }
    }

    /* property 定义结束 */

    Item {
        id: container
        anchors.fill: parent
        anchors.topMargin: 10

        ColumnLayout {
            anchors.fill: parent

            // 按钮部分
            Item {
                id: checkedBtnContainer
                Layout.fillWidth: true
                Layout.preferredHeight: 48

                RowLayout {
                    anchors {
                        fill: parent
                        leftMargin: 16
                    }
                    spacing: 10

                    // 全部
                    CheckableButton {
                        defaultText: "全部: " + quanBuCount.toString()
                        checkedText: "全部: " + quanBuCount.toString()
                        fontColor: "#FF222222"
                        checkedFontColor: "#FF2F42CD"
                        checkedBorderColor: "#FF2F42CD"
                        isChecked: quanBuCheck
                        onClicked: {
                            filterBtnClicked(0)
                        }
                    }

                    // mini-STR数量
                    CheckableButton {
                        defaultText: "mini-STR数量: " + miniSTRCount.toString()
                        checkedText: "mini-STR数量: " + miniSTRCount.toString()
                        fontColor: "#FF222222"
                        checkedFontColor: "#FF2F42CD"
                        checkedBorderColor: "#FF2F42CD"
                        isChecked: miniSTRCheck
                        onClicked: {
                            filterBtnClicked(1)
                        }
                    }

                    // Y-STR数量
                    CheckableButton {
                        defaultText: "Y-STR数量: " + ySTRCount.toString()
                        checkedText: "Y-STR数量: " + ySTRCount.toString()
                        fontColor: "#FF222222"
                        checkedFontColor: "#FF2F42CD"
                        checkedBorderColor: "#FF2F42CD"
                        isChecked: ySTRCheck
                        onClicked: {
                            filterBtnClicked(2)
                        }
                    }

                    // 需稀释样品数量
                    CheckableButton {
                        defaultText: "需稀释样品数量: " + xuXiShiCount.toString()
                        checkedText: "需稀释样品数量: " + xuXiShiCount.toString()
                        fontColor: "#FF222222"
                        checkedFontColor: "#FF2F42CD"
                        checkedBorderColor: "#FF2F42CD"
                        isChecked: xuXiShiCheck
                        onClicked: {
                            filterBtnClicked(3)
                        }
                    }

                    CustomHorSpacerItem {}

                }
            }

            // 表格部分
            Item {
                id: tableContainer
                Layout.fillWidth: true
                Layout.fillHeight: true

                // 表头部分
                // 横向表头
                Item {
                    id: horHeaderFlickable
                    width: parent.width
                    height: _horHeaderHeight

                    Item {
                        id: horizontalHeader
                        anchors.fill: parent

                        Row {
                            id: horHeaderRowLayout
                            spacing: 0

                            // 起始表头
                            Repeater {
                                id: startHorHeaderItemRepeater
                                model: root._startHorHeaderData

                                Item {
                                    width: horizontalHeader.width / (_startHorHeaderData.length +
                                                                     _endHorHeaderData.length +
                                                                     _secondHorHeaderData.length)
                                    implicitHeight: horizontalHeader.height

                                    Rectangle {
                                        id: startHorHeaderBg
                                        anchors.fill: parent
                                        border.width: 0
                                        color: "transparent"
                                        clip: true

                                        Text {
                                            id: startHorHeaderText
                                            text: qsTr(modelData)
                                            anchors.centerIn: parent
                                            font.pixelSize: 14
                                            font.family: "Alibaba PuHuiTi"
                                            color: "#FF222222"
                                            leftPadding: 15
                                            rightPadding: 15
                                            horizontalAlignment: Text.AlignHCenter
                                            verticalAlignment: Text.AlignVCenter
                                            wrapMode: Text.WordWrap
                                        }
                                    }

                                    // 画边框
                                    Canvas {
                                        id: startHorHeaderCanvas
                                        antialiasing: true
                                        anchors.fill: parent
                                        onPaint: {
                                            var ctx = getContext("2d")
                                            ctx.clearRect(0, 0, width, height)
                                            ctx.strokeStyle = "#FF999999"
                                            ctx.lineWidth = 2
                                            ctx.beginPath()

                                            // 上边框
                                            ctx.moveTo(0, 0)
                                            ctx.lineTo(width, 0)
                                            ctx.stroke()

                                            // 下边框
                                            ctx.moveTo(0, height)
                                            ctx.lineTo(width, height)
                                            ctx.stroke()
                                        }
                                    }

                                    // 左边框
                                    Canvas {
                                        id: startHorHeaderLeftBorder
                                        visible: index !== 0
                                        antialiasing: true
                                        anchors.fill: parent
                                        onPaint: {
                                            // 左边框
                                            var ctx = getContext("2d")
                                            ctx.clearRect(0, 0, width, height)
                                            ctx.strokeStyle = "#FF999999"
                                            ctx.lineWidth = 2
                                            ctx.beginPath()
                                            ctx.moveTo(0, 0)
                                            ctx.lineTo(0, height)
                                            ctx.stroke()
                                        }
                                    }

                                }
                            }

                            // 多级表头
                            Item {
                                id: multiHorHeaderItemCon
                                width: horizontalHeader.width / (root._startHorHeaderData.length +
                                                                 root._endHorHeaderData.length +
                                                                 root._secondHorHeaderData.length) *
                                       root._secondHorHeaderData.length
                                height: horizontalHeader.height

                                Column {
                                    id: multiHeaderColLayout
                                    spacing: 0

                                    // 一级表头
                                    Row {
                                        id: firstHeaderRowLayout
                                        spacing: 0

                                        Repeater {
                                            id: firstHorHeaderItemRepeater
                                            model: root._firstHorHeaderData

                                            Item {
                                                width: multiHorHeaderItemCon.width /
                                                       root._firstHorHeaderData.length
                                                implicitHeight: root._firstHorHeaderHeight

                                                Rectangle {
                                                    id: firstHorHeaderBg
                                                    anchors.fill: parent
                                                    border.width: 0
                                                    color: "transparent"
                                                    clip: true

                                                    Text {
                                                        id: firstHorHeaderText
                                                        text: qsTr(modelData)
                                                        anchors.centerIn: parent
                                                        font.pixelSize: 14
                                                        font.family: "Alibaba PuHuiTi"
                                                        color: "#FF222222"
                                                        leftPadding: 15
                                                        rightPadding: 15
                                                        horizontalAlignment: Text.AlignHCenter
                                                        verticalAlignment: Text.AlignVCenter
                                                        wrapMode: Text.WordWrap
                                                    }
                                                }

                                                // 画边框
                                                Canvas {
                                                    id: firstHorHeaderCanvas
                                                    antialiasing: true
                                                    anchors.fill: parent
                                                    onPaint: {
                                                        var ctx = getContext("2d")
                                                        ctx.clearRect(0, 0, width, height)
                                                        ctx.strokeStyle = "#FF999999"
                                                        ctx.lineWidth = 2
                                                        ctx.beginPath()

                                                        // 上边框
                                                        ctx.moveTo(0, 0)
                                                        ctx.lineTo(width, 0)
                                                        ctx.stroke()

                                                        // 下边框
                                                        ctx.moveTo(0, height)
                                                        ctx.lineTo(width, height)
                                                        ctx.stroke()

                                                        // 左边框
                                                        ctx.moveTo(0, 0)
                                                        ctx.lineTo(0, height)
                                                        ctx.stroke()
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    // 二级表头
                                    Row {
                                        id: secondHeaderRowLayout
                                        spacing: 0

                                        Repeater {
                                            id: secondHorHeaderItemRepeater
                                            model: root._secondHorHeaderData

                                            Item {
                                                width: multiHorHeaderItemCon.width /
                                                       root._secondHorHeaderData.length
                                                implicitHeight: root._secondHorHeaderHeight

                                                Rectangle {
                                                    id: secondHorHeaderBg
                                                    anchors.fill: parent
                                                    border.width: 0
                                                    color: "transparent"
                                                    clip: true

                                                    Text {
                                                        id: secondHorHeaderText
                                                        text: qsTr(modelData)
                                                        anchors.centerIn: parent
                                                        font.pixelSize: 14
                                                        font.family: "Alibaba PuHuiTi"
                                                        color: "#FF222222"
                                                        leftPadding: 15
                                                        rightPadding: 15
                                                        horizontalAlignment: Text.AlignHCenter
                                                        verticalAlignment: Text.AlignVCenter
                                                        wrapMode: Text.WordWrap
                                                    }
                                                }

                                                // 画边框
                                                Canvas {
                                                    id: secondHorHeaderCanvas
                                                    antialiasing: true
                                                    anchors.fill: parent
                                                    onPaint: {
                                                        var ctx = getContext("2d")
                                                        ctx.clearRect(0, 0, width, height)
                                                        ctx.strokeStyle = "#FF999999"
                                                        ctx.lineWidth = 2
                                                        ctx.beginPath()

                                                        // 下边框
                                                        ctx.moveTo(0, height)
                                                        ctx.lineTo(width, height)
                                                        ctx.stroke()

                                                        // 左边框
                                                        ctx.moveTo(0, 0)
                                                        ctx.lineTo(0, height)
                                                        ctx.stroke()
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            // 末尾表头
                            Repeater {
                                id: endHorHeaderItemRepeater
                                model: root._endHorHeaderData

                                Item {
                                    width: horizontalHeader.width / (_startHorHeaderData.length +
                                                                     _endHorHeaderData.length +
                                                                     _secondHorHeaderData.length)
                                    implicitHeight: horizontalHeader.height

                                    Rectangle {
                                        id: endHorHeaderBg
                                        anchors.fill: parent
                                        border.width: 0
                                        color: "transparent"
                                        clip: true

                                        Text {
                                            id: endHorHeaderText
                                            text: qsTr(modelData)
                                            anchors.centerIn: parent
                                            font.pixelSize: 14
                                            font.family: "Alibaba PuHuiTi"
                                            color: "#FF222222"
                                            leftPadding: 15
                                            rightPadding: 15
                                            horizontalAlignment: Text.AlignHCenter
                                            verticalAlignment: Text.AlignVCenter
                                            wrapMode: Text.WordWrap
                                        }
                                    }

                                    // 画边框
                                    Canvas {
                                        id: endHorHeaderCanvas
                                        antialiasing: true
                                        anchors.fill: parent
                                        onPaint: {
                                            var ctx = getContext("2d")
                                            ctx.clearRect(0, 0, width, height)
                                            ctx.strokeStyle = "#FF999999"
                                            ctx.lineWidth = 2
                                            ctx.beginPath()

                                            // 上边框
                                            ctx.moveTo(0, 0)
                                            ctx.lineTo(width, 0)
                                            ctx.stroke()

                                            // 下边框
                                            ctx.moveTo(0, height)
                                            ctx.lineTo(width, height)
                                            ctx.stroke()

                                            // 左边框
                                            ctx.moveTo(0, 0)
                                            ctx.lineTo(0, height)
                                            ctx.stroke()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // 表格部分
                TableView {
                    id: tableContent
                    height: parent.height - horHeaderFlickable.height
                    anchors {
                        left: parent.left
                        right: parent.right
                        top: parent.top
                        topMargin: _horHeaderHeight
                    }
                    clip: true
                    boundsBehavior: Flickable.StopAtBounds
                    columnSpacing: 0
                    rowSpacing: 0

                    columnWidthProvider: function(column){
                        return container.width / (_startHorHeaderData.length +
                                                  _endHorHeaderData.length +
                                                  _secondHorHeaderData.length)
                    }

                    rowHeightProvider: function(row){
                        return _tableItemHeight
                    }

                    model: TableModel {
                        id: tableModel

                        TableModelColumn {display: "序号"}
                        TableModelColumn {display: "孔位"}
                        TableModelColumn {display: "样本名称"}
                        TableModelColumn {display: "检测方案"}
                        TableModelColumn {display: "总稀释方案DFa"}
                        TableModelColumn {display: "稀释倍数1DFa1"}
                        TableModelColumn {display: "样品体积(μl)"}
                        TableModelColumn {display: "稀释液体积1(μl)"}
                        TableModelColumn {display: "稀释倍数2DFa2"}
                        TableModelColumn {display: "上一步稀释产物(μl)"}
                        TableModelColumn {display: "稀释液体积2(μl)"}
                        TableModelColumn {display: "扩增上样体积"}
                        TableModelColumn {display: "备注"}

                        rows: _tableModel
                    }

                    delegate: Rectangle {
                        id: tableDelegate
                        color: "transparent"
                        width: tableContent.columnWidthProvider(column)
                        height: tableContent.rowHeightProvider(0)
                        implicitWidth: tableItem.width
                        border.width: 0
                        clip: true

                        Label {
                            id: tableItem
                            anchors.fill: parent
                            font.family: "Alibaba PuHuiTi"
                            font.pixelSize: 14
                            color: "#FF222222"
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                            text: qsTr(display && (typeof display === "string") ? display : "")
                            antialiasing: true
                            clip: true
                            elide: Text.ElideRight

                            // 文本过长，显示ToolTips
                            Label {
                                id: innerTableItem
                                visible: false
                                text: parent.text
                            }

                            ToolTip {
                                id: tooltip
                                visible: innerTableItem.width > tableItem.width &&
                                         itemDeleMouseArea.containsMouse
                                x: 0
                                y: tableDelegate.height
                                z: 999
                                text: parent.text
                                font: parent.font
                            }
                        }

                        // 边框
                        Canvas {
                            anchors.fill: parent
                            antialiasing: true
                            onPaint: {
                                var ctx = getContext("2d")
                                ctx.strokeStyle = "#FF999999"
                                ctx.lineWidth = 2
                                // 下边框
                                ctx.moveTo(0, height)
                                ctx.lineTo(width, height)
                                ctx.stroke()
                            }
                        }

                        // 左边框
                        Canvas {
                            anchors.fill: parent
                            antialiasing: true
                            visible: column !== 0
                            onPaint: {
                                var ctx = getContext("2d")
                                ctx.strokeStyle = "#FF999999"
                                ctx.lineWidth = 2
                                ctx.moveTo(0, 0)
                                ctx.lineTo(0, height)
                                ctx.stroke()
                            }
                        }

                        // 鼠标事件
                        MouseArea {
                            id: itemDeleMouseArea
                            anchors.fill: parent
                            hoverEnabled: true
                        }
                    }

                    // 事件
                    onHeightChanged: {
                        forceLayout() // 强制重新布局刷新视图
                    }
                }
            }
        }
    }
}
